//
//  BaseTimeLabel.m
//  DuBai
//
//  Created by darktm on 14-2-23.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "BaseTimeLabel.h"


@interface BaseTimeLabel ()

@end

@implementation BaseTimeLabel

@synthesize seconds = _seconds;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    
    return self;
}
-(NSTimeInterval)seconds{
    
    
    return _seconds <= 0 ? 0 : _seconds;
}

-(void)setSeconds:(NSTimeInterval)seconds{
    
    _seconds = seconds;
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(on_timer_change) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:UITrackingRunLoopMode];
}

-(void)on_timer_change{
    
//    NSLog(@"on_timer_change");
    _seconds--;
    
    if(_seconds <= 0){
        
        self.enabled = NO;
        [self.timer invalidate];
        self.timer = nil;
    }
    [self updateLableValue];
}

-(void)updateLableValue{
    
    self.text = [NSString stringWithFormat:@"%.0f",_seconds];
}

-(void)dealloc{
    
    if([self.timer isValid]){
        [self.timer invalidate];
        self.timer = nil;
    }
    NSLog(@"%@",[self class]);
}


@end
